/**
 * 
 */
package vn.lumi.smarthome.network;

import java.util.Arrays;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Schedule;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.util.Log;

/**
 * @author Chu Tuan Anh
 *
 */
public class ToggleSchedule extends AppMsg{

	//constants
	private static final int LENGTH = 6;
	
	//variables
	private int scheduleId;
	private int scheduleState;
	
	public ToggleSchedule( int msg_id, int uid, int sid,int id, int state){
		super(NetMessage.CMD_APP_TOGGLE_SCHEDULE, msg_id,uid,sid);
		this.scheduleId = id;
		this.scheduleState = state;
	}
	
	/* (non-Javadoc)
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel dest, int arg1) {
		// TODO Auto-generated method stub
		dest.writeInt(this.scheduleId);
		dest.writeInt(this.scheduleState);
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#generateByteArray()
	 */
	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub
		byte[] result = new byte[LENGTH];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Msg_id;
		result[2] = (byte) this.user_id;
		result[3] = (byte) this.session_id;
		result[4] = (byte) this.scheduleId;
		result[5] = (byte) this.scheduleState;
		Log.e("ban tin","Toggle Schedule: " + Arrays.toString(result));
		return result;
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfSendSuccessful(android.content.Context)
	 */
	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put(TBL_Schedule.STATE, this.scheduleState);
		String selection = new String(TBL_Schedule._ID+"=?");
		String[] selectionArgs = new String[]{this.scheduleId+""};
		context.getContentResolver().update(TBL_Schedule.CONTENT_URI_SCHEDULE, values, selection, selectionArgs);
		
		//Hieu them vao
				//inform UI
			    Intent intent = new Intent();
			    intent.setAction(context.getString(R.string.txt_broadcast_Toogle_schedule_successful));
			    context.sendBroadcast(intent);
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfSendFail(android.content.Context)
	 */
	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		//Hieu them vao
		//inform UI
	    Intent intent = new Intent();
	    intent.setAction(context.getString(R.string.txt_broadcast_Toogle_schedule_failed));
	    context.sendBroadcast(intent);
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#readFromParcel(android.os.Parcel)
	 */
	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		this.scheduleId = in.readInt();
		this.scheduleState = in.readInt();
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectReply(byte[], int)
	 */
	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		HC_ACK ack = new HC_ACK();
		if(ack.isCorrectFormat(input, length)){
			ack.parseMessage(input, length);
			if(ack.getMsg_id() == this.Msg_id) result = true;
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result =false;
		HC_ACK_BUSY ack_busy = new HC_ACK_BUSY();
		if(ack_busy.isCorrectFormatBusy(input, length)){
			ack_busy.parseMessage(input, length);
			if(ack_busy.getMsg_id() == this.Msg_id) result = true;
		}
		return result; 
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub
		
	}
	

}
